package com.intretech.umsin.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.intretech.umsin.system.converter.FileInfoVoEntityConverter;
import com.intretech.umsin.system.entity.FileInfoEntity;
import com.intretech.umsin.system.mapper.FileInfoMapper;
import com.intretech.umsin.system.param.FileInfoParam;
import com.intretech.umsin.system.service.FileInfoService;
import com.intretech.umsin.system.vo.FileInfoVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;



/**
* @version V1.0
* 文件信息:file_info 对应商业服务实现.
* @ClassName: FileInfoServiceImpl
* @Description: 文件信息:file_info 对应商业服务实现.
* @author: 李启联
* @date:   2022-8-6 14:37:21
* @Copyright: Copyright (c) 2022
*/
@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfoEntity> implements FileInfoService {

    @Autowired
    private FileInfoVoEntityConverter fileInfoVoEntityConverter;


    /**
    * 根据ID查询单个文件信息对象
    * @param id 组键ID
    * @return 单个文件信息VO对象
    */
    public FileInfoVo queryById(Serializable id){
        if (id == null) {return null;}
        return fileInfoVoEntityConverter.reverseFromTarget(this.getById(id));
    }


    /**
    * 根据ID集合查询返回文件信息列表
    * @param idList 组键ID集合
    * @return 文件信息列表
    */
    public List<FileInfoVo> queryByIds(Collection<? extends Serializable> idList){
        if(CollectionUtils.isEmpty(idList)){return null;}
        return fileInfoVoEntityConverter.reverseFromTarget(this.listByIds(idList));
    }


    /**
    * 分页查询 文件信息 信息
    * @param page 分页对象
    * @param param 查询参数
    * @return IPage<FileInfoVo> 分页对象
    */
    @Override
    public IPage<FileInfoVo> queryForPage(IPage page,FileInfoParam param){
      IPage<FileInfoEntity> page0 = this.page(page,createQueryWrapper(param));
      IPage<FileInfoVo> pageResult = page0.convert(e -> fileInfoVoEntityConverter.reverseFromTarget(e));
      return pageResult;
    }


    /**
    * 根据条件查询返回 文件信息 对象列表
    * @param  param 查询参数
    * @return 文件信息 对象列表
    */
    @Override
    public List<FileInfoVo> queryForList(FileInfoParam param) {
        List<FileInfoEntity> list = this.list(createQueryWrapper(param));
        return fileInfoVoEntityConverter.reverseFromTarget(list);
    }

    /**
    * 构建查询条件
    * @param param 查询参数实体对象
    * @return 构建后的QueryWrapper查询条件
    */
    private QueryWrapper<FileInfoEntity> createQueryWrapper(FileInfoParam param){
        QueryWrapper<FileInfoEntity> queryWrapper = new QueryWrapper<>();
        if(ObjectUtils.isEmpty(param)) { return queryWrapper; }
        Optional<FileInfoParam> op = Optional.of(param);
        queryWrapper.lambda()
                .eq(op.map(FileInfoParam::getId).isPresent(),FileInfoEntity::getId,param.getId())
                .eq(op.map(FileInfoParam::getName).filter(StringUtils::isNotBlank).isPresent(),FileInfoEntity::getName,param.getName())
                .eq(op.map(FileInfoParam::getIsImg).isPresent(),FileInfoEntity::getIsImg,param.getIsImg())
                .eq(op.map(FileInfoParam::getContentType).filter(StringUtils::isNotBlank).isPresent(),FileInfoEntity::getContentType,param.getContentType())
                .eq(op.map(FileInfoParam::getSize).isPresent(),FileInfoEntity::getSize,param.getSize())
                .eq(op.map(FileInfoParam::getFileType).filter(StringUtils::isNotBlank).isPresent(),FileInfoEntity::getFileType,param.getFileType())
                .eq(op.map(FileInfoParam::getPath).filter(StringUtils::isNotBlank).isPresent(),FileInfoEntity::getPath,param.getPath())
                .eq(op.map(FileInfoParam::getUrl).filter(StringUtils::isNotBlank).isPresent(),FileInfoEntity::getUrl,param.getUrl())
                .eq(op.map(FileInfoParam::getSource).filter(StringUtils::isNotBlank).isPresent(),FileInfoEntity::getSource,param.getSource())
                .eq(op.map(FileInfoParam::getBusinessId).filter(StringUtils::isNotBlank).isPresent(),FileInfoEntity::getBusinessId,param.getBusinessId())
                .eq(op.map(FileInfoParam::getUserDefine).filter(StringUtils::isNotBlank).isPresent(),FileInfoEntity::getUserDefine,param.getUserDefine())
                .eq(op.map(FileInfoParam::getTagsName).filter(StringUtils::isNotBlank).isPresent(),FileInfoEntity::getTagsName,param.getTagsName())
        ;
        return queryWrapper;
    }

    /**
    * 添加文件信息到数据库
    * @param vo  文件信息 VO对象
    * @return 添加后的文件信息 VO对象
    */
    @Override
    public FileInfoVo add(FileInfoVo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        FileInfoEntity entity = fileInfoVoEntityConverter.convertToTarget(vo);
        if(this.save(entity)){
            return fileInfoVoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            throw new RuntimeException("添加 文件信息 单个对象失败!");
            //return null;
        }
    }

    /**
    * 添加文件信息集合列表到数据库
    * @param vos  文件信息 VO对象列表
    * @return 添加后的文件信息 VO对象集合列表
    */
    @Override
    public List<FileInfoVo> adds(List<FileInfoVo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<FileInfoEntity> entities = fileInfoVoEntityConverter.convertToTarget(vos);
        if(this.saveBatch(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return fileInfoVoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
           throw new RuntimeException("添加 文件信息 集合列表失败!");
           //return null;
        }
    }

    /**
    * 更新文件信息到数据库
    * @param vo 要更新的文件信息 VO对象
    * @return 更新后的文件信息 VO对象
    */
    @Override
    public FileInfoVo modify(FileInfoVo vo) {
        if (ObjectUtils.isEmpty(vo)) {return null;}
        FileInfoEntity entity = fileInfoVoEntityConverter.convertToTarget(vo);
        if(this.updateById(entity)){
            return fileInfoVoEntityConverter.reverseFromTarget(this.getById(entity.getId()));
        }else {
            throw new RuntimeException("更新文件信息单个对象失败!");
            //return null;
        }
    }

    /**
    * 更新文件信息集合列表到数据库
    * @param vos 要更新的文件信息 VO对象集合
    * @return 更新后的文件信息 VO对象集合
    */
    @Override
    public List<FileInfoVo> modifys(List<FileInfoVo> vos) {
        if (CollectionUtils.isEmpty(vos)) { return null; }
        List<FileInfoEntity> entities = fileInfoVoEntityConverter.convertToTarget(vos);
        if(this.updateBatchById(entities)){
            List<Serializable> ids = new ArrayList<>();
            entities.forEach(e->{ids.add(e.getId());});
            return fileInfoVoEntityConverter.reverseFromTarget(this.listByIds(ids));
        }else {
            throw new RuntimeException("更新文件信息集合列表失败!");
            //return null;
        }
    }	

}

